import '../../amap.dart';

class SearchTextResult {
  bool success;
  int count;
  String info;
  String infoCode;
  List<PoiBean> poiList;

  SearchTextResult({
    this.count,
    this.info,
    this.infoCode,
    this.poiList,
    this.success,
  });

  factory SearchTextResult.fromJson(Map<String, dynamic> json) {
    return SearchTextResult(
      count: int.tryParse(json['count']) ?? 0,
      info: json['info'],
      infoCode: json['infocode'],
      poiList: json['pois'] != null
          ? (json['pois'] as List).map((i) => PoiBean.fromJson(i)).toList()
          : null,
      success: json['status'] == "1",
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['count'] = this.count;
    data['info'] = this.info;
    data['infocode'] = this.infoCode;
    data['status'] = this.success ? "1" : 0;
    if (this.poiList != null) {
      data['pois'] = this.poiList.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class PoiBean {
  String id;
  String name;
  String address;
  String pname;
  String cityname;
  String adname;
  LatLng location;
  String typecode;

  PoiBean({
    this.address,
    this.adname,
    this.cityname,
    this.id,
    this.location,
    this.name,
    this.pname,
    this.typecode,
  });

  factory PoiBean.fromJson(Map<String, dynamic> json) {
    final latLng = json['location']?.toString()?.split(",");
    if (latLng == null || latLng.length != 2) return PoiBean();
    print("xxxxx $json");
    return PoiBean(
      address: json['address'],
      adname: json['adname'],
      cityname: json['cityname'],
      id: json['id'],
      location: LatLng(
          double.tryParse(latLng[1]), double.tryParse(latLng[0]), json["name"]),
      name: json['name'],
      pname: json['pname'],
      typecode: json['typecode'],
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['address'] = this.address;
    data['adname'] = this.adname;
    data['cityname'] = this.cityname;
    data['id'] = this.id;
    data['location'] = this.location?.toJson();
    data['name'] = this.name;
    data['pname'] = this.pname;
    data['typecode'] = this.typecode;
    return data;
  }
}
